// Copyright (C) ZHENG Zhong <http://www.zhengzhong.net/>
//
// $Id: transformer.hpp 318 2009-03-02 14:03:21Z heavyzheng $
//

#ifndef ZZHENG_XML_XSLT_LIBXSLT_TRANSFORMER_HPP_20090206__
#define ZZHENG_XML_XSLT_LIBXSLT_TRANSFORMER_HPP_20090206__

#include <zzheng/xml/config.hpp>

#include <zzheng/xml/xslt/libxslt/base.hpp>
#include <zzheng/xml/xslt/libxslt/libxslt_fwd.hpp>
#include <zzheng/xml/xslt/libxslt/xslt_result.hpp>

#include <boost/shared_ptr.hpp>
#include <string>


namespace zzheng {
namespace xml {
namespace xslt {
namespace libxslt_ {

    //! This class represents an XSLT transformer.
    class transformer: private base {

    public:

        //! Default constructor. Constructs an empty (unloaded) XSLT transformer.
        explicit transformer();

        // Use auto-generated destructor.

        //! Checks if an XSLT stylesheet has been loaded in this transformer.
        //! \return true if an XSLT has been loaded in this transformer, false otherwise.
        bool loaded() const;

        //! Loads an XSLT stylesheet from an XML file.
        //! \param file_name  the XSLT stylesheet file to load.
        //! \throws xslt_error  if fail to load the XSLT file.
        void load_from_file(const std::string& file_name);

        //! Loads an XSLT stylesheet from an XML string.
        //! \param str  the XSLT stylesheet string to load.
        //! \throws xslt_error  if fail to load the XSLT string.
        void load_from_string(const char* str);

        //! Transforms an XML file by applying the stylesheet on it.
        //! \param file_name  the XML file to transform.
        //! \return the transformed result, always good.
        //! \throws xslt_error  if failed to apply the stylesheet on the XML file.
        xslt_result transform_file(const std::string& file_name);

        //! Transforms an XML string by applying the stylesheet on it.
        //! \param str  the XML string to transform.
        //! \return the transformed result, always good.
        //! \throws xslt_error  if failed to apply the stylesheet on the XML string.
        xslt_result transform_string(const char* str);

    private:

        //! Non-implemented copy constructor.
        transformer(const transformer&);

        //! Non-implemented copy assignment.
        transformer& operator=(const transformer&);

        //! Loads an XSLT stylesheet from a libxml2 document.
        //! \param doc  the libxml2 document to load.
        //! \return true if the XSLT stylesheet is loaded successfully, false otherwise.
        bool load_from_xmldoc(xmlDoc* doc);

        //! Transforms a libxml2 document by applying the stylesheet on it. If the transformation
        //! fails, this function returns a bad XSLT result, but does not throw an exception.
        //! \param doc  the libxml2 document to transform.
        //! \return the transformed result, maybe good, maybe bad.
        xslt_result transform_xmldoc(xmlDoc* doc);

    private:

        boost::shared_ptr<xsltStylesheet> stylesheet_; //!< The libxslt stylesheet.

    }; // class transformer

} // namespace zzheng::xml::xslt::libxslt_
} // namespace zzheng::xml::xslt
} // namespace zzheng::xml
} // namespace zzheng

#endif // ZZHENG_XML_XSLT_LIBXSLT_TRANSFORMER_HPP_20090206__




